package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.activity.ProductsActivity;
import com.da.lon.wang.xlg.bean.SeckillResultBean.SeckillItemContentBean;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.util.TimeCount;
import com.da.lon.wang.xlg.util.Utils;
import com.da.lon.wang.xlg.view.TimeDownView;

import java.util.ArrayList;

public class TimeLimitAdapter extends BaseAdapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private LayoutInflater inflater;
    private Context context;
    private ArrayList<SeckillItemContentBean> list;
    private Holder holder;
    private long server_time;
    private long start_time;
    private long end_time;
    private TimeCount time;
    private TextView timedown_hour;
    private TextView timedown_min;
    private TextView timedown_second;
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == XLGConstants.TIMEDOWN) {
                if ((long) msg.obj == 0L) {
                    // 发送刷新 秒杀列表
                    Intent intent = new Intent();
                    intent.setAction(XLGConstants.UPDATE_SKILL);
                    context.sendBroadcast(intent);
                }
                Utils.showTimeDownView((long) msg.obj, timedown_hour,
                        timedown_min, timedown_second);

            }

        }

        ;
    };
    private TextView skill_status_tv;

    public TimeLimitAdapter(Context context,
                            ArrayList<SeckillItemContentBean> list, long server_time,
                            long start_time, long end_time) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.list = list;
        this.server_time = server_time;
        this.start_time = start_time;
        this.end_time = end_time;
    }

    @Override
    public int getCount() {
        return list != null && list.size() != 0 ? list.size() + 1 : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        int INDEX = 0;
        if (position == 0) {
            INDEX = TYPE_ITEM;
        } else {
            INDEX = TYPE_SEPARATOR;
        }
        return INDEX;

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {

        return 2;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        try {
            int type = getItemViewType(position);
            if (convertView == null) {
                holder = new Holder();
                switch (type) {
                    case TYPE_ITEM:
                        convertView = inflater.inflate(
                                R.layout.ouka_time_limit_lv_header, null);

                        holder.padding_end_ev = (TextView) convertView
                                .findViewById(R.id.padding_end_ev);
                        // holder.sk_timedownview = (TimeDownView) convertView
                        // .findViewById(R.id.sk_timedownview);

                        timedown_hour = (TextView) convertView
                                .findViewById(R.id.timedown_hour);
                        timedown_min = (TextView) convertView
                                .findViewById(R.id.timedown_min);
                        timedown_second = (TextView) convertView
                                .findViewById(R.id.timedown_second);

                        skill_status_tv = (TextView) convertView
                                .findViewById(R.id.skill_status_tv);

                        if (server_time < start_time) {
                            holder.padding_end_ev.setText("距开始 ");
                            skill_status_tv.setText("即将开始,先下单先得哦");

                            time = new TimeCount((start_time - server_time) * 1000,
                                    1000, mHandler);
                        } else if (server_time > start_time
                                && start_time < end_time) {
                            holder.padding_end_ev.setText("距结束 ");
                            // holder.sk_timedownview.setTimes(end_time -
                            // server_time);
                            skill_status_tv.setText("抢购中,先下单先得哦");
                            time = new TimeCount((end_time - server_time) * 1000,
                                    1000, mHandler);
                        }

                        time.start();
                        break;
                    case TYPE_SEPARATOR:
                        convertView = inflater.inflate(R.layout.ouka_time_limit_lv,
                                null);

                        holder.skill_item_ll = (LinearLayout) convertView
                                .findViewById(R.id.skill_item_ll);
                        holder.skill_price_tv = (TextView) convertView
                                .findViewById(R.id.skill_price_tv);
                        holder.skill_name_tv = (TextView) convertView
                                .findViewById(R.id.skill_name_tv);
                        holder.yishou_tv = (TextView) convertView
                                .findViewById(R.id.yishou_tv);
                        holder.jingdu = (ProgressBar) convertView
                                .findViewById(R.id.jingdu);
                        holder.skill_iv = (ImageView) convertView
                                .findViewById(R.id.skill_iv);
                        holder.bpTextView = (TextView) convertView
                                .findViewById(R.id.beforeprice);
                        holder.bpTextView.getPaint().setFlags(
                                Paint.STRIKE_THRU_TEXT_FLAG);

                        holder.qgTextView = (TextView) convertView
                                .findViewById(R.id.qianggou);

                        holder.qianggou = (TextView) convertView
                                .findViewById(R.id.qianggou);

                        break;
                }
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }

            switch (type) {
                case TYPE_ITEM:

                    // if (!holder.sk_timedownview.isRun()) {
                    // holder.sk_timedownview.run();
                    // }
                    break;

                case TYPE_SEPARATOR:
                    // int seckilled_num = list.get(position -
                    // 1).getSeckilled_num();
                    // int seckill_num = list.get(position - 1).getSeckill_num();
                    // float percentage = (float) seckilled_num / (float)
                    // seckill_num;
                    // percentage = percentage * 100;
                    // int p = (int) percentage;
                    holder.yishou_tv.setText(list.get(position - 1).getSale_scale()
                            + "%");
                    holder.jingdu.setMax(list.get(position - 1).getSeckill_num());
                    holder.jingdu.setProgress(list.get(position - 1)
                            .getSeckilled_num());
                    MyApplication.loadImageByPicasso(context, list
                                    .get(position - 1).getBigimage(), null,
                            holder.skill_iv, 0, 0, 1);
                    String seckill_price = list.get(position - 1)
                            .getSeckill_price() == null ? "0" : list.get(
                            position - 1).getSeckill_price();
                    holder.skill_price_tv.setText("￥" + seckill_price);
                    String price = list.get(position - 1).getPrice() == null ? "0"
                            : list.get(position - 1).getPrice();
                    holder.bpTextView.setText("￥" + price);
                    holder.skill_name_tv.setText(list.get(position - 1).getName());
                    // 0 待抢购 1去抢购 2抢完了
                    if (list.get(position - 1).getStatus().equals("2")) {
                        holder.qgTextView
                                .setBackgroundResource(R.drawable.ouka_textview_style2);
                        holder.qgTextView.setText("已抢完");
                        holder.qgTextView.setTextColor(0xFFFF4B56);
                        holder.jingdu.setVisibility(View.GONE);
                        holder.yishou_tv.setVisibility(View.VISIBLE);
                        holder.yishou_tv.setText("已抢完拉!下次再来吧!");
                    } else if (list.get(position - 1).getStatus().equals("0")) {
                        holder.qgTextView
                                .setBackgroundResource(R.drawable.ouka_skill_style);
                        holder.qgTextView.setText("待秒杀");
                        // holder.qgTextView.setTextColor(0xFFFF4B56);
                        holder.jingdu.setVisibility(View.GONE);
                        holder.yishou_tv.setVisibility(View.GONE);
                    } else if (list.get(position - 1).getStatus().equals("1")) {
                        holder.qgTextView.setText("去抢购");
                        holder.jingdu.setVisibility(View.VISIBLE);
                        holder.yishou_tv.setVisibility(View.VISIBLE);
                    }
                    holder.skill_item_ll.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            Intent intent = new Intent(context,
                                    ProductsActivity.class);
                            String product_number = list.get(position - 1)
                                    .getProduct_number();
                            PreferenceUtils.getInstance().SetSettingString(
                                    PreferencesConfig.PRODUCT_NUMBER,
                                    product_number);
                            context.startActivity(intent);
                        }
                    });

                    break;
                default:
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

    class Holder {

        public TextView timedown_second;
        public TextView timedown_min;
        public TextView timedown_hour;
        public TextView skill_price_tv;
        public LinearLayout skill_item_ll;
        public TextView qianggou;
        public TimeDownView sk_timedownview;
        public TextView padding_end_ev;
        public TextView skill_name_tv;
        public TextView yishou_tv;
        public ProgressBar jingdu;
        public ImageView skill_iv;
        TextView bpTextView;
        TextView qgTextView;
    }

}
